package com.neusoft.zqygbc.neusoft_wenda.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SearchView;
import android.widget.Toast;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.neusoft.zqygbc.neusoft_wenda.R;
import com.neusoft.zqygbc.neusoft_wenda.activity.LoginActivity;
import com.neusoft.zqygbc.neusoft_wenda.activity.NewQuestionActivity;
import com.neusoft.zqygbc.neusoft_wenda.activity.SearchActivity;
import com.neusoft.zqygbc.neusoft_wenda.adapter.CreateQuestionAdapter;
import com.neusoft.zqygbc.neusoft_wenda.adapter.QListAdapter;
import com.neusoft.zqygbc.neusoft_wenda.base.RetrofitCallback;
import com.neusoft.zqygbc.neusoft_wenda.javeBean.MyQuestionList;
import com.neusoft.zqygbc.neusoft_wenda.javeBean.QuestionEntity;
import com.neusoft.zqygbc.neusoft_wenda.javeBean.QuestionList;
import com.neusoft.zqygbc.neusoft_wenda.model.QusetionModel;
import com.neusoft.zqygbc.neusoft_wenda.service.QuestionService;
import com.neusoft.zqygbc.neusoft_wenda.view.SwipyRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static android.content.Context.MODE_PRIVATE;


/**
 * Created by dell on 2016/6/20.
 */
public class AllFragment extends Fragment  implements SwipyRefreshLayout.OnRefreshListener  {

    SharedPreferences sid;
    private RecyclerView recyerview;
    private SwipyRefreshLayout refreshLayout;
    private LinearLayoutManager linearLayoutManager;
    private int refreshTime = 0;
    private int times = 0;
    private int page = 1;
    private final int TOP_REFRESH = 1;
    private final int BOTTOM_REFRESH = 2;
    private ArrayList<QuestionEntity> list = new ArrayList();
    private FloatingActionButton home_fbtn;
    private QuestionList result;
    private QListAdapter adapter;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_all, null);
        initViews(view);
        initEvents();
        initData(1);
        return view;
    }

    private void initViews(View view) {
        sid = getActivity().getSharedPreferences("userinfo", Context.MODE_PRIVATE);
        home_fbtn = (FloatingActionButton) view.findViewById(R.id.home_fbtn);
        recyerview = (RecyclerView) view.findViewById(R.id.recyerview);
        refreshLayout = (SwipyRefreshLayout) view.findViewById(R.id.refreshLayout);
        refreshLayout.setOnRefreshListener(this);
        linearLayoutManager = new LinearLayoutManager(this.getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyerview.setLayoutManager(linearLayoutManager);
        adapter = new QListAdapter(AllFragment.this.getContext(), list);
        recyerview.setAdapter(adapter);//设置适配器

    }

    private void initEvents() {
        home_fbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (sid.getString("user_id", "").equals("")) {
                    Toast.makeText(getActivity(), "尚未登录", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(getActivity(), LoginActivity.class);
                    startActivity(intent);
                } else {
                    Intent intent = new Intent(getActivity(), NewQuestionActivity.class);
                    startActivity(intent);
                }
            }
        });



    }

    private void initData(int page) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://115.159.93.244/wenda/api.php/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        QuestionService questionService = retrofit.create(QuestionService.class);
        Call<QuestionList> call = questionService.getQuestionList(page);
        call.enqueue(new Callback<QuestionList>() {
            @Override
            public void onResponse(Call<QuestionList> call, Response<QuestionList> response) {
                result = response.body();
                list.addAll(result.getQuestion());
                adapter.notifyDataSetChanged();
                refreshLayout.setRefreshing(false);
            }

            @Override
            public void onFailure(Call<QuestionList> call, Throwable t) {
                refreshLayout.setRefreshing(false);
            }
        });
    }

    @Override
    public void onRefresh(int index) {
        dataOption(TOP_REFRESH);
        Toast.makeText(getActivity(), "已经是最新数据", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onLoad(int index) {
        dataOption(BOTTOM_REFRESH);
        Toast.makeText(getActivity(), "加载完成", Toast.LENGTH_SHORT).show();
    }

    private void dataOption(int option) {
        switch (option) {
            case TOP_REFRESH:
                //下拉刷新
                list.clear();
                page = 1;
                initData(page);
                break;
            case BOTTOM_REFRESH:
                //上拉加载更多
                page++;
                initData(page);
                break;
        }
        // adapter.notifyDataSetChanged();

    }
}
